package com.tancire.member.service.impl;

import java.util.List;
import javax.annotation.Resource;

import cn.hutool.core.util.ObjectUtil;
import com.tancire.common.exception.question.TrainException;
import com.tancire.common.utils.DateUtils;
import com.tancire.common.utils.SecurityUtils;
import com.tancire.question.domain.TrainActivity;
import com.tancire.question.service.ITrainActivityService;
import org.springframework.stereotype.Service;
import com.tancire.member.mapper.MemberTrainActivityMapper;
import com.tancire.member.domain.MemberTrainActivity;
import com.tancire.member.service.IMemberTrainActivityService;

/**
 * 活动参加记录Service业务层处理
 *
 * @author tancire
 * @date 2024-10-27
 */
@Service
public class MemberTrainActivityServiceImpl implements IMemberTrainActivityService {

    @Resource
    private MemberTrainActivityMapper memberTrainActivityMapper;

    @Resource
    private ITrainActivityService trainActivityService;

    /**
     * 查询活动参加记录
     *
     * @param id 活动参加记录主键
     * @return 活动参加记录
     */
    @Override
    public MemberTrainActivity selectMemberTrainActivityById(Long id) {
        return memberTrainActivityMapper.selectMemberTrainActivityById(id);
    }

    /**
     * 查询活动参加记录列表
     *
     * @param memberTrainActivity 活动参加记录
     * @return 活动参加记录
     */
    @Override
    public List<MemberTrainActivity> selectMemberTrainActivityList(MemberTrainActivity memberTrainActivity) {
        return memberTrainActivityMapper.selectMemberTrainActivityList(memberTrainActivity);
    }

    /**
     * 新增活动参加记录
     *
     * @param memberTrainActivity 活动参加记录
     * @return 结果
     */
    @Override
    public int insertMemberTrainActivity(MemberTrainActivity memberTrainActivity) {
        memberTrainActivity.setCreateTime(DateUtils.getNowDate());
        return memberTrainActivityMapper.insertMemberTrainActivity(memberTrainActivity);
    }

    /**
     * 修改活动参加记录
     *
     * @param memberTrainActivity 活动参加记录
     * @return 结果
     */
    @Override
    public int updateMemberTrainActivity(MemberTrainActivity memberTrainActivity) {
        memberTrainActivity.setUpdateTime(DateUtils.getNowDate());
        return memberTrainActivityMapper.updateMemberTrainActivity(memberTrainActivity);
    }

    /**
     * 批量删除活动参加记录
     *
     * @param ids 需要删除的活动参加记录主键
     * @return 结果
     */
    @Override
    public int deleteMemberTrainActivityByIds(Long[] ids) {
        return memberTrainActivityMapper.deleteMemberTrainActivityByIds(ids);
    }

    /**
     * 删除活动参加记录信息
     *
     * @param id 活动参加记录主键
     * @return 结果
     */
    @Override
    public int deleteMemberTrainActivityById(Long id) {
        return memberTrainActivityMapper.deleteMemberTrainActivityById(id);
    }

    /**
     * 开启答题接口
     *
     * @param activityId
     * @return
     */
    @Override
    public MemberTrainActivity startActivity(Long activityId) {
        if (null == activityId) {
            throw new TrainException("活动不能为空！");
        }
        // 查询活动信息
        TrainActivity trainActivity = this.trainActivityService.selectTrainActivityById(activityId);
        this.validateActivity(trainActivity);
        MemberTrainActivity startActivity = new MemberTrainActivity();
        MemberTrainActivity query = new MemberTrainActivity();
        query.setActivityId(activityId);
        query.setMemberId(SecurityUtils.getUserId());
        List<MemberTrainActivity> memberTrainActivities = this.memberTrainActivityMapper.selectMemberTrainActivityList(query);
        if (ObjectUtil.isEmpty(memberTrainActivities)) {
            startActivity.setMemberId(SecurityUtils.getUserId());
            startActivity.setActivityId(activityId);
            startActivity.setActivityName(trainActivity.getActivityName());
            startActivity.setAttendStatus(1);
            this.memberTrainActivityMapper.insertMemberTrainActivity(startActivity);
        } else {
            MemberTrainActivity memberTrainActivity = memberTrainActivities.get(0);
            if (null != memberTrainActivity.getAttendStatus() && 1 == memberTrainActivity.getAttendStatus()) {
                throw new TrainException("此活动您已经参加！");
            }
        }
        return startActivity;
    }

    /**
     * 交卷
     *
     * @param memberTrainActivity
     */
    @Override
    public void answerSubmit(MemberTrainActivity memberTrainActivity) {
        Long activityId = memberTrainActivity.getActivityId();
        // 查询活动信息
        TrainActivity trainActivity = this.trainActivityService.selectTrainActivityById(activityId);
        this.validateActivity(trainActivity);
        MemberTrainActivity query = new MemberTrainActivity();
        query.setActivityId(activityId);
        query.setMemberId(SecurityUtils.getUserId());
        List<MemberTrainActivity> memberTrainActivities = this.memberTrainActivityMapper.selectMemberTrainActivityList(query);
        if (ObjectUtil.isEmpty(memberTrainActivities)) {
            throw new TrainException("此活动您未参加，不能交卷！");
        }
        MemberTrainActivity memberTrainActivityInfo = memberTrainActivities.get(0);
        // 保存答题时间
        memberTrainActivityInfo.setStartTime(memberTrainActivity.getStartTime());
        memberTrainActivityInfo.setUseTime(memberTrainActivity.getUseTime());
        // 更新数据
        this.memberTrainActivityMapper.updateMemberTrainActivity(memberTrainActivityInfo);
    }

    /**
     * 查询已参加的活动列表数据
     *
     * @return
     */
    @Override
    public List<String> getAttendList() {
        return this.memberTrainActivityMapper.getAttendList(SecurityUtils.getUserId());
    }

    /**
     * 校验活动信息
     *
     * @param trainActivity
     */
    private void validateActivity(TrainActivity trainActivity) {
        if (null == trainActivity || null == trainActivity.getId()) {
            throw new TrainException("活动不存在！");
        }
        if (!DateUtils.timeBetween(trainActivity.getActivityStart(), trainActivity.getActivityEnd())) {
            throw new TrainException("活动未在比赛期间内！");
        }
    }
}
